﻿// <copyright file="MapPanel.xaml.cs" company="Taasss">Copyright (c) 2009 All Right Reserved</copyright>
// <author>Ben Vincent</author>
// <date>2010-02-21</date>
// <summary>MapPanel</summary>
namespace Fotofly.PhotoViewer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    using Fotofly.PhotoViewer.FotoflyWebService;

    using Microsoft.Maps.MapControl;
    using Microsoft.Maps.MapControl.Core;

    public partial class MapPanel : UserControl
    {
        private MapLayer mapLayer;
        private List<MapPhotoPanel> mapPhotoPanels = new List<MapPhotoPanel>();
        private string bingCredentialsProvider;
        private Uri imageUri;
        private double iconWidth;
        private double iconHeight;

        public MapPanel(double mapWidth, double mapHeight, Uri imageUri, double iconWidth, double iconHeight)
        {
            this.InitializeComponent();

            // Save image details
            this.imageUri = imageUri;
            this.iconHeight = iconHeight;
            this.iconWidth = iconWidth;

            // Set Credentials Provider
            this.bingMap.Width = mapWidth;
            this.bingMap.Height = mapHeight;

            // Add Photo Layer
            this.mapLayer = new MapLayer();
            this.mapLayer.Visibility = Visibility.Visible;
            this.mapLayer.Name = "Photos";
            this.mapLayer.SetValue(Canvas.ZIndexProperty, 100);

            this.bingMap.Children.Add(this.mapLayer);
        }

        public string BingCredentialsProvider
        {
            get
            {
                return this.bingCredentialsProvider;
            }

            set
            {
                this.bingCredentialsProvider = value;
                this.bingMap.CredentialsProvider = new ApplicationIdCredentialsProvider(this.bingCredentialsProvider);
            }
        }

        public List<MapPhotoPanel> MapPhotoPanels
        {
            get
            {
                return this.mapPhotoPanels;
            }
        }

        public void SetIconCoordinates(GpsPosition gpsPosition)
        {
            if (!string.IsNullOrEmpty(this.BingCredentialsProvider) && gpsPosition.IsValidCoordinate)
            {
                // Create a location
                Location pinLocation = new Location(gpsPosition.Latitude.Numeric, gpsPosition.Longitude.Numeric, gpsPosition.Altitude);

                // Add the image
                MapPhotoPanel mapPhotoPanel = new MapPhotoPanel(this.imageUri, this.iconWidth, this.iconHeight);
                mapPhotoPanels.Add(mapPhotoPanel);

                // Add it to the map
                this.mapLayer.AddChild(mapPhotoPanel, pinLocation, PositionOrigin.BottomCenter);

                this.bingMap.SetView(pinLocation, 7);
            }
        }
    }
}
